#include <iostream>
#include <ctime>
#include <cstring>
#include <string>

using namespace std;

enum according
{
    NAME,
    PRICE,
    AMOUNT
};

typedef struct
{
    char name[8];
    float price;
    int amount;
} Drink;

char DreakName[5][8] = {"cola", "Coffee", "Tea", "Water", "Juice"};

void DrinkArrInit(Drink *drink, int n);                // 对数组进行赋值
void DrinkArrPrint(Drink *drink, int n);               // 打印数组
void DrinkArrSort(Drink *drink, int n, according acc); // 根据传入的枚举值排序
void swap_drink(Drink &drink1, Drink &drink2);         // 交换函数

string find_max_price(Drink *p, int n);

int main()
{
    Drink *drink = new Drink[5];
    DrinkArrInit(drink, 5);
    cout << "原始数据" << endl;
    DrinkArrPrint(drink, 5);

    cout << "按名称排序" << endl;
    DrinkArrSort(drink, 5, NAME);
    DrinkArrPrint(drink, 5);

    cout << "最贵的饮料是" << find_max_price(drink, 5) << endl;
    cout << "按价格排序" << endl;
    DrinkArrSort(drink, 5, PRICE);
    DrinkArrPrint(drink, 5);

    cout << "按数量排序" << endl;
    DrinkArrSort(drink, 5, AMOUNT);
    DrinkArrPrint(drink, 5);
    delete[] drink;
    return 0;
}

void DrinkArrInit(Drink *drink, int n)
{
    for (int i = 0; i < n; i++)
    {
        strcpy(drink[i].name, DreakName[i]);
    }
    srand(time(0));
    for (int i = 0; i < n; i++)
    {
        drink[i].price = (float)(rand() % 100 + 1) / 10.0;
        drink[i].amount = rand() % 100;
    }
}

void DrinkArrPrint(Drink *drink, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << drink[i].name << "\t" << drink[i].price << "  " << drink[i].amount << endl;
    }
}
void DrinkArrSort(Drink *drink, int n, according acc)
{
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            switch (acc)
            {
            case NAME:
                if (strcmp(drink[j].name, drink[j + 1].name) > 0)
                {
                    swap_drink(drink[j], drink[j + 1]);
                }
                break;

            case PRICE:
                if (drink[j].price > drink[j + 1].price)
                {
                    swap_drink(drink[j], drink[j + 1]);
                }
                break;

            case AMOUNT:
                if (drink[j].amount > drink[j + 1].amount)
                {
                    swap_drink(drink[j], drink[j + 1]);
                }
                break;

            default:
                break;
            }
        }
    }
}

void swap_drink(Drink &drink1, Drink &drink2)
{
    auto temp = drink1;
    drink1 = drink2;
    drink2 = temp;
}

string find_max_price(Drink *p, int n)
{
    string maxname; //接收最贵饮料名称，作为返回值
    int max = 0;    //记录最贵物品下标
    for (int i = 1; i < 5; i++)
    {
        if ((p + max)->price < (p + i)->price)
            max = i;
    }
    maxname = (p + max)->name;  //转换成string字符串
    return maxname;
}







